//
//  SendTweetController.m
//  NewsReader
//
//  Created by blacksun on 11-3-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SendTweetController.h"
#import "CustomDetailViewController.h"
#import "NewsReaderAppDelegate.h"
#import "URLAddress.h"


@implementation SendTweetController

@synthesize backButton;
@synthesize sendButton;
@synthesize contentView;
@synthesize weibo;
@synthesize content;
@synthesize countLabel;
@synthesize feed;
@synthesize notifyView;


-(IBAction)send:(id)sender{
	NSString *text = self.contentView.text;
    
    NSURL *url = [[NSURL alloc] initWithString:[URLAddress sendTweetUrl:self.feed.feedId]];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];//send a log to my server;
    
    [request release];
    [url release];
    
	[weibo sendTweet:text];//send tweets;
	[self backToDetail];
    [self notifySendSuccess];
}
-(IBAction)back:(id)sender{
	[self backToDetail];
}

-(void) backToDetail{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	self.view.frame = CGRectMake(0.0, 460.0, 320.0, 460.0);
	[self.contentView resignFirstResponder];
	[UIView commitAnimations];
	[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
	
	[self setScrollAble:YES];
	
}
-(void) notifySendSuccess{
    UIView *parentView = [self.view superview];
    [parentView addSubview:self.notifyView];
    [UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:2.0f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
    self.notifyView.alpha = 0.0;
    [UIView commitAnimations];
    
    [self.notifyView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.1];
    
    
}


-(void) setScrollAble:(BOOL)scrollAble{
	NewsReaderAppDelegate *delegate = [UIApplication sharedApplication].delegate;
	CustomDetailViewController *topController = (CustomDetailViewController *)[delegate.navigationController visibleViewController];
	topController.scrollView.scrollEnabled = scrollAble;
}
-(void) setRemainderCount:(NSInteger)currentCount{
	NSInteger remainder = 140 - currentCount;
	countLabel.text = [NSString stringWithFormat:@"%d",remainder];
	if (remainder >0) {
		countLabel.textColor = [UIColor whiteColor];
	}else {
		countLabel.textColor = [UIColor redColor];
	}

}

- (void)textViewDidChange:(UITextView *)textView{
	[self setRemainderCount:[contentView.text length]];
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentView.layer.cornerRadius = 6.0;
    self.contentView.layer.masksToBounds = YES;
	self.contentView.text = content;
	self.contentView.delegate = self;
	[self setRemainderCount:[content length]];
    
    self.notifyView.layer.cornerRadius = 6.0;
    self.notifyView.layer.masksToBounds = YES;
    [self.notifyView removeFromSuperview];

}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	self.backButton = nil;
	self.sendButton = nil;
	self.contentView = nil;
	self.weibo = nil;
	self.content = nil;
	self.countLabel = nil;
    self.feed = nil;
    self.notifyView = nil;
    [super dealloc];
}


@end